package com.ll.tankgame;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Vector;

/**
 * @author liubw
 * @date 2024/6/24 0024
 */
public class MyPanel extends JPanel implements KeyListener,Runnable {

    Hero hero = null;
    Vector<Enemy> enemys = new Vector<>();
    int enemySize = 3;
    public  MyPanel(){
        hero = new Hero(100,100);
        hero.setSpeed(5);
        for (int i =0;i < enemySize;i++){
            Enemy enemy = new Enemy(100 * (i + 1), 0);
            enemy.setDirection(2);
            enemys.add(enemy);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.fillRect(0,0,1000,750);

        drawTank(hero.getX(),hero.getY(),g,hero.getDirection(),hero.getType());
        if (hero.shot != null && hero.shot.isLive == true){

            g.draw3DRect(hero.shot.getX(),hero.shot.getY(),5,5,false);
        }

        for (int i = 0;i < enemySize;i++){
            Enemy enemy = enemys.get(i);

            drawTank(enemy.getX(),enemy.getY(),g,enemy.getDirection(),enemy.getType());
        }

    }

    public void drawTank(int x , int y ,Graphics g,int direction,int type){
        switch (type){
            case 0:
                g.setColor(Color.cyan);
                break;
            case 1:
                g.setColor(Color.yellow);
                break;
        }
        switch (direction){
            case 0:
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x + 30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+30,x+20,y);
                break;
            case 1:
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+ 30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+30,y+20,x+60,y+20);
                break;
            case 2:
                g.fill3DRect(x,y,10,60,false);
                g.fill3DRect(x + 30,y,10,60,false);
                g.fill3DRect(x+10,y+10,20,40,false);
                g.fillOval(x+10,y+20,20,20);
                g.drawLine(x+20,y+30,x+20,y+60);
                break;
            case 3:
                g.fill3DRect(x,y,60,10,false);
                g.fill3DRect(x,y+ 30,60,10,false);
                g.fill3DRect(x+10,y+10,40,20,false);
                g.fillOval(x+20,y+10,20,20);
                g.drawLine(x+30,y+20,x,y+20);
                break;
        }


    }


    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_W){
            hero.setDirection(0);
            hero.moveUp();
        }else if (e.getKeyCode() == KeyEvent.VK_D){
            hero.setDirection(1);
            hero.moveRight();
        }else if (e.getKeyCode() == KeyEvent.VK_S){
            hero.setDirection(2);
            hero.moveDown();
        }else if (e.getKeyCode() == KeyEvent.VK_A){
            hero.setDirection(3);
            hero.moveLeft();
        }
        if (e.getKeyCode()==KeyEvent.VK_J){
            hero.shotEnemyTank();
        }

        this.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            this.repaint();
        }
    }
}
